package com.ymttest.business.service;

import java.math.BigDecimal;
import java.util.Random;

import com.ymatou.iapi.trading.operate.parameter.Order;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymt.core.tool.Logger;

public class Trading {

	private OrderCallService orderService = new OrderCallService();

	/**
	 *交易全流程 
	 */
	public void trading() {
		try {
			// 创建订单

			PlaceOrderResp placeOrderResp = placeOrder();

			Order order = placeOrderResp.getOrderList().get(0);

			int sellerId = order.getSellerId();
			int orderId = order.getOrderId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal payAmount = placeOrderResp.getPayAmount();

			int i = new Random().nextInt(10);

			if (i < 3) {

				// 买家付款
				orderService.orderPayRequest(userId, payAmount, tradingId);

			}

			/*			Thread.sleep(3000);

						orderService.cancelOrder(orderId, userId,
								CancelOrderTypeEnum.BuyerCancelOrder);*/
			if (i >= 3 && i < 8) {

				// 买家付款
				orderService.orderPayRequest(userId, payAmount, tradingId);
				// 接单
				orderService.acceptOrder(orderId, sellerId);
			}

			if (i >= 6 && i < 8) {

				// 买家付款
				orderService.orderPayRequest(userId, payAmount, tradingId);
				// 接单
				orderService.acceptOrder(orderId, sellerId);

				//商家发货 
				orderService.dispatchedByXlobo(orderId, sellerId);
			}

			if (i >= 8) {

				// 买家付款
				orderService.orderPayRequest(userId, payAmount, tradingId);
				// 接单
				orderService.acceptOrder(orderId, sellerId);

				//商家发货 
				orderService.dispatched(orderId, sellerId);
				// 确认收货
				orderService.confirmReceive(userId, orderId);
			}

			//Logger.info("orderId:" + orderId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public PlaceOrderResp placeOrder() {

		PlaceOrderReq placeorderBean = PlaceOrderService.getDefaultOrderReq();

		int i = new Random().nextInt(10);

		if (i < 3) {

			placeorderBean.getOrderGroups().get(0).getOrders().get(0)
					.getOrderItems().get(0)
					.setLogisticsType(LogisticsTypeEnum.BondedDelivery);
		}

		if (i >= 3 && i < 8) {
			placeorderBean.getOrderGroups().get(0).getOrders().get(0)
					.getOrderItems().get(0)
					.setLogisticsType(LogisticsTypeEnum.SailProtect);
		}
		if (i >= 8) {
			placeorderBean.getOrderGroups().get(0).getOrders().get(0)
					.getOrderItems().get(0)
					.setLogisticsType(LogisticsTypeEnum.PackageDelivery);
		}

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}
	public static void main(String[] args) {

		Trading t = new Trading();

		Logger.setDebug(false);

		//多少个线程
		for (int i = 0; i < 5; i++) {
			Logger.info("******************Thread " + (i + 1)
					+ "*******************");
			new Thread(() -> {
				try {
					//单个线程跑多少次
					for (int j = 0; j < 1500; j++) {
						Logger.info("******************" + j
								+ "*******************");
						t.trading();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}).start();

		}

	}
}
